package com.rongcheng.frc.newstyledemo.ui.player.video;

import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.MediaController;


import com.rongcheng.frc.newstyledemo.ui.player.constants.MediaPlayerStatus;

import java.io.IOException;

/**
 * Created by Administrator on 2015/5/27.
 */
public class YanxiuVideoView extends SurfaceView implements MediaController.MediaPlayerControl {
    private String TAG = "VideoView";
    private Uri mUri;
    private int mDuration;


    // mCurrentState is a VideoView object's current state.
    // mTargetState is the state that a method caller intends to reach.
    // For instance, regardless the VideoView object's current state,
    // calling pause() intends to bring the object to a target state
    // of STATE_PAUSED.
    private int mCurrentState = MediaPlayerStatus.STATE_IDLE;
    private int mTargetState = MediaPlayerStatus.STATE_IDLE;

    private final int FORWARD_TIME = 15000;
    private final int REWIND_TIME = 15000;

    // All the stuff we need for playing and showing a video
    private SurfaceHolder mSurfaceHolder = null;
    private MediaPlayer mMediaPlayer = null;
    private Context mContext;
    private int mVideoWidth;
    private int mVideoHeight;
    private int mSurfaceWidth;
    private int mSurfaceHeight;
    private MediaController mMediaController;
    private MediaPlayer.OnCompletionListener mOnCompletionListener;
    private MediaPlayer.OnPreparedListener mOnPreparedListener;
    private MediaPlayer.OnInfoListener mOnInfoListener;
    private VideoViewStateChangeListener mStateChangeListener;
    private int mCurrentBufferPercentage;
    private MediaPlayer.OnErrorListener mOnErrorListener;
    private boolean fullScreen;
    private int mSeekWhenPrepared;
    private boolean mCanPause;
    private boolean mCanSeekBack;
    private boolean mCanSeekForward;

    protected boolean isBufferCount = true;
    protected int lastSeekWhenPrepared = 0;
    private boolean enforcementWait = false;
    private boolean enforcementPause = false;

    public YanxiuVideoView(Context context) {
        super(context);
        this.mContext = context;
        initVideoView();
    }

    public YanxiuVideoView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        this.mContext = context;
        initVideoView();
    }

    public YanxiuVideoView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.mContext = context;
        initVideoView();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = getDefaultSize(mVideoWidth, widthMeasureSpec);
        int height = getDefaultSize(mVideoHeight, heightMeasureSpec);
        if (!fullScreen) {
            if (mVideoWidth > 0 && mVideoHeight > 0) {
                if (mVideoWidth * height > width * mVideoHeight) {

                    height = width * mVideoHeight / mVideoWidth;
                } else if (mVideoWidth * height < width * mVideoHeight) {
                    width = height * mVideoWidth / mVideoHeight;
                } else {
                }
            }
        }
        setMeasuredDimension(width, height);
    }

    private void invalateScreenSize() {
        fullScreen = !fullScreen;
        ViewGroup.LayoutParams lp = (ViewGroup.LayoutParams) this.getLayoutParams();
        this.setLayoutParams(lp);
    }

    public int resolveAdjustedSize(int desiredSize, int measureSpec) {
        int result = desiredSize;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        switch (specMode) {
            case MeasureSpec.UNSPECIFIED:
            /*
             * Parent says we can be as big as we want. Just don't be larger
			 * than max size imposed on ourselves.
			 */
                result = desiredSize;
                break;

            case MeasureSpec.AT_MOST:
			/*
			 * Parent says we can be as big as we want, up to specSize. Don't be
			 * larger than specSize, and don't be larger than the max size
			 * imposed on ourselves.
			 */
                result = Math.min(desiredSize, specSize);
                break;

            case MeasureSpec.EXACTLY:
                result = specSize;
                break;
        }
        return result;
    }

    private void initVideoView() {
        mVideoWidth = 0;
        mVideoHeight = 0;
        getHolder().addCallback(mSHCallback);
        setFocusable(true);
        setFocusableInTouchMode(true);
        requestFocus();
        mCurrentState = MediaPlayerStatus.STATE_IDLE;
        StateChange(mCurrentState);
        mTargetState = MediaPlayerStatus.STATE_IDLE;
    }

    public void setVideoPath(String path) {
        setVideoURI(Uri.parse(path));
    }

    public void setVideoURI(Uri uri) {
        mUri = uri;
        mSeekWhenPrepared = 0;
        openVideo();
        requestLayout();
        invalidate();
        // setVisibility(View.VISIBLE);
    }

    public void stopPlayback() {
        StateChange(MediaPlayerStatus.STATE_STOPBACK);// 涓虹粺璁″姞鍏�
        if (mMediaPlayer != null) {
            mMediaPlayer.stop();
            mMediaPlayer.release();
            mMediaPlayer = null;
            mCurrentState = MediaPlayerStatus.STATE_IDLE;
            StateChange(mCurrentState);
            mTargetState = MediaPlayerStatus.STATE_IDLE;
            setVisibility(View.INVISIBLE);
        }
    }

    private void openVideo() {
        if (mUri == null) {
            // not ready for playback just yet, will try again later
            return;
        }

        if (mSurfaceHolder == null) {
            getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
            setVisibility(VISIBLE);
            return;
        }
        // Tell the music playback service to pause
        // TODO: these constants need to be published somewhere in the
        // framework.
        Intent i = new Intent("com.android.music.musicservicecommand");
        i.putExtra("command", "pause");
        mContext.sendBroadcast(i);

        // we shouldn't clear the target state, because somebody might have
        // called start() previously
        release(false);
        try {
//			if (NativeInfos.ifNativePlayer()) {
//				mMediaPlayer = new NativePlayer();
//			} else {
            mMediaPlayer = new MediaPlayer();
//			}
            mMediaPlayer.setOnPreparedListener(mPreparedListener);
            mMediaPlayer.setOnInfoListener(mInfoListener);
            mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener);
            mDuration = -1;
            mMediaPlayer.setOnCompletionListener(mCompletionListener);
            mMediaPlayer.setOnErrorListener(mErrorListener);
            mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener);
            mCurrentBufferPercentage = 0;
            mMediaPlayer.setDataSource(mContext, mUri);
            mMediaPlayer.setDisplay(mSurfaceHolder);
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mMediaPlayer.setScreenOnWhilePlaying(true);
            mMediaPlayer.prepareAsync();
            // we don't set the target state here either, but preserve the
            // target state that was there before.
            mCurrentState = MediaPlayerStatus.STATE_PREPARING;
            StateChange(mCurrentState);
            attachMediaController();
        } catch (IOException ex) {
            Log.w(TAG, "Unable to open content: " + mUri, ex);
            mCurrentState = MediaPlayerStatus.STATE_ERROR;
            StateChange(mCurrentState);
            mTargetState = MediaPlayerStatus.STATE_ERROR;
            mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            return;
        } catch (IllegalArgumentException ex) {
            Log.w(TAG, "Unable to open content: " + mUri, ex);
            mCurrentState = MediaPlayerStatus.STATE_ERROR;
            StateChange(mCurrentState);
            mTargetState = MediaPlayerStatus.STATE_ERROR;
            mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
            return;
        }
    }

    public void setMediaController(MediaController controller) {
        if (mMediaController != null) {
            mMediaController.hide();
        }
        mMediaController = controller;
        attachMediaController();
    }

    // public void setCancelActivity(CancelActivity mCancelActivity){

    // this.mCancelActivity = mCancelActivity;
    // }

    private void attachMediaController() {
        if (mMediaPlayer != null && mMediaController != null) {
            mMediaController.setMediaPlayer(this);
            View anchorView = this.getParent() instanceof View ? (View) this.getParent() : this;
            mMediaController.setAnchorView(anchorView);
            mMediaController.setEnabled(isInPlaybackState());
        }
    }

    MediaPlayer.OnVideoSizeChangedListener mSizeChangedListener = new MediaPlayer.OnVideoSizeChangedListener() {
        public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
            mVideoWidth = mp.getVideoWidth();
            mVideoHeight = mp.getVideoHeight();
            if (mVideoWidth != 0 && mVideoHeight != 0) {
                getHolder().setFixedSize(mVideoWidth, mVideoHeight);
            }
        }
    };


    MediaPlayer.OnInfoListener mInfoListener = new MediaPlayer.OnInfoListener() {
        @Override
        public boolean onInfo(MediaPlayer mediaPlayer, int i, int i1) {
            mCurrentState = MediaPlayerStatus.STATE_INFO;
            StateChange(mCurrentState);
            return false;
        }
    };


    MediaPlayer.OnPreparedListener mPreparedListener = new MediaPlayer.OnPreparedListener() {
        public void onPrepared(MediaPlayer mp) {
            mCurrentState = MediaPlayerStatus.STATE_PREPARED;
            StateChange(mCurrentState);

            // // Get the capabilities of the player for this stream
            // Metadata data = mp.getMetadata(MediaPlayer.METADATA_ALL,
            // MediaPlayer.BYPASS_METADATA_FILTER);
            //
            // if (data != null) {
            // mCanPause = !data.has(Metadata.PAUSE_AVAILABLE)
            // || data.getBoolean(Metadata.PAUSE_AVAILABLE);
            // mCanSeekBack = !data.has(Metadata.SEEK_BACKWARD_AVAILABLE)
            // || data.getBoolean(Metadata.SEEK_BACKWARD_AVAILABLE);
            // mCanSeekForward = !data.has(Metadata.SEEK_FORWARD_AVAILABLE)
            // || data.getBoolean(Metadata.SEEK_FORWARD_AVAILABLE);
            // } else {
            // mCanPause = mCanSeekForward = mCanSeekForward = true;
            // }

            mCanPause = mCanSeekBack = mCanSeekForward = true;

            if (mOnPreparedListener != null) {
                mOnPreparedListener.onPrepared(mMediaPlayer);
            }
            if (mMediaController != null) {
                mMediaController.setEnabled(true);
            }
            mVideoWidth = mp.getVideoWidth();
            mVideoHeight = mp.getVideoHeight();

            int seekToPosition = mSeekWhenPrepared; // mSeekWhenPrepared may be
            // changed after seekTo()
            // call
            if (seekToPosition != 0) {
                seekTo(seekToPosition);
            }
            if (mVideoWidth != 0 && mVideoHeight != 0) {
                // Log.i("@@@@", "video size: " + mVideoWidth +"/"+
                // mVideoHeight);
                getHolder().setFixedSize(mVideoWidth, mVideoHeight);
                if (mSurfaceWidth == mVideoWidth && mSurfaceHeight == mVideoHeight) {
                    // We didn't actually change the size (it was already at the
                    // size
                    // we need), so we won't get a "surface changed" callback,
                    // so
                    // start the video here instead of in the callback.
                    if (mTargetState == MediaPlayerStatus.STATE_PLAYING) {
                        start();
                        if (mMediaController != null) {
                            mMediaController.show();
                        }
                    } else if (!isPlaying() && (seekToPosition != 0 || getCurrentPosition() > 0)) {
                        if (mMediaController != null) {
                            // Show the media controls when we're paused into a
                            // video and make 'em stick.
                            mMediaController.show(0);
                        }
                    }
                }
            } else {
                // We don't know the video size yet, but should start anyway.
                // The video size might be reported to us later.
                if (mTargetState == MediaPlayerStatus.STATE_PLAYING) {
                    start();
                }
            }
        }
    };

    private MediaPlayer.OnCompletionListener mCompletionListener = new MediaPlayer.OnCompletionListener() {
        public void onCompletion(MediaPlayer mp) {
            mCurrentState = MediaPlayerStatus.STATE_PLAYBACK_COMPLETED;
            StateChange(mCurrentState);
            mTargetState = MediaPlayerStatus.STATE_PLAYBACK_COMPLETED;
            mCurrentState = MediaPlayerStatus.STATE_STOPBACK;
            StateChange(mCurrentState);// 鏈粺璁″姞鍏�
            if (mMediaController != null) {
                mMediaController.hide();
            }
            if (mOnCompletionListener != null) {
                mOnCompletionListener.onCompletion(mMediaPlayer);
            }
        }
    };

    private MediaPlayer.OnErrorListener mErrorListener = new MediaPlayer.OnErrorListener() {
        public boolean onError(MediaPlayer mp, int framework_err, int impl_err) {
            Log.d(TAG, "Error: " + framework_err + "," + impl_err);
            mCurrentState = MediaPlayerStatus.STATE_ERROR;
            StateChange(mCurrentState);
            mTargetState = MediaPlayerStatus.STATE_ERROR;
            if (mMediaController != null) {
                mMediaController.hide();
            }

			/* If an error handler has been supplied, use it and finish. */
            if (mOnErrorListener != null) {
                if (mOnErrorListener.onError(mMediaPlayer, framework_err, impl_err)) {
                    return true;
                }
            }

			/*
			 * Otherwise, pop up an error dialog so the user knows that
			 * something bad has happened. Only try and pop up the dialog if
			 * we're attached to a window. When we're going away and no longer
			 * have a window, don't bother showing the user an error.
			 */
            // if (getWindowToken() != null) {
            //
            // // Resources r = mContext.getResources();
            // // int messageId;
            // //
            // // if (framework_err ==
            // // MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK) {
            // // messageId =
            // //
            // com.android.internal.R.string.VideoView_error_text_invalid_progressive_playback;
            // // } else {
            // // messageId =
            // // com.android.internal.R.string.VideoView_error_text_unknown;
            // // }
            // //
            // // new AlertDialog.Builder(mContext)
            // // .setTitle(com.android.internal.R.string.VideoView_error_title)
            // // .setMessage(messageId)
            // //
            // .setPositiveButton(com.android.internal.R.string.VideoView_error_button,
            // // new DialogInterface.OnClickListener() {
            // // public void onClick(DialogInterface dialog, int whichButton)
            // // {
            // // /* If we get here, there is no onError listener, so
            // // * at least inform them that the video is over.
            // // */
            // // if (mOnCompletionListener != null) {
            // // mOnCompletionListener.onCompletion(mMediaPlayer);
            // // }
            // // }
            // // })
            // // .setCancelable(false)
            // // .show();
            // }
            return true;
        }
    };

    private MediaPlayer.OnBufferingUpdateListener mBufferingUpdateListener = new MediaPlayer.OnBufferingUpdateListener() {
        public void onBufferingUpdate(MediaPlayer mp, int percent) {
            mCurrentBufferPercentage = percent;
        }
    };

    /**
     * Register a callback to be invoked when the media file is loaded and ready
     * to go.
     *
     * @param l The callback that will be run
     */
    public void setOnPreparedListener(MediaPlayer.OnPreparedListener l) {
        mOnPreparedListener = l;
    }

    /**
     * Register a callback to be invoked when the end of a media file has been
     * reached during playback.
     *
     * @param l The callback that will be run
     */
    public void setOnCompletionListener(MediaPlayer.OnCompletionListener l) {
        mOnCompletionListener = l;
    }

    /**
     * Register a callback to be invoked when an error occurs during playback or
     * setup. If no listener is specified, or if the listener returned false,
     * VideoView will inform the user of any errors.
     *
     * @param l The callback that will be run
     */
    public void setOnErrorListener(MediaPlayer.OnErrorListener l) {
        mOnErrorListener = l;
    }

    SurfaceHolder.Callback mSHCallback = new SurfaceHolder.Callback() {
        public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
            mSurfaceWidth = w;
            mSurfaceHeight = h;
            boolean isValidState = (mTargetState == MediaPlayerStatus.STATE_PLAYING);
            boolean hasValidSize = (mVideoWidth == w && mVideoHeight == h);
            if (mMediaPlayer != null && isValidState && hasValidSize) {
                if (mSeekWhenPrepared != 0) {
                    seekTo(mSeekWhenPrepared);
                }
                start();
                if (mMediaController != null) {
                    mMediaController.show();
                }
            }
        }

        public void surfaceCreated(SurfaceHolder holder) {
            mSurfaceHolder = holder;
            openVideo();
        }

        public void surfaceDestroyed(SurfaceHolder holder) {
            // after we return from this we can't use the surface any more
            mSurfaceHolder = null;
            if (mMediaController != null)
                mMediaController.hide();
            lastSeekWhenPrepared = getCurrentPosition();
            release(true);
        }
    };

    /*
     * release the media player in any state
     */
    private void release(boolean cleartargetstate) {
        if (mMediaPlayer != null) {
            mMediaPlayer.reset();
            mMediaPlayer.release();
            mMediaPlayer = null;
            mCurrentState = MediaPlayerStatus.STATE_IDLE;
            StateChange(mCurrentState);
            if (cleartargetstate) {
                mTargetState = MediaPlayerStatus.STATE_IDLE;
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (isInPlaybackState() && mMediaController != null) {
            toggleMediaControlsVisiblity();
        }
        return false;
    }

    @Override
    public boolean onTrackballEvent(MotionEvent ev) {
        if (isInPlaybackState() && mMediaController != null) {
            toggleMediaControlsVisiblity();
        }
        return false;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        boolean isKeyCodeSupported = keyCode != KeyEvent.KEYCODE_BACK
                && keyCode != KeyEvent.KEYCODE_VOLUME_UP && keyCode != KeyEvent.KEYCODE_VOLUME_DOWN
                && keyCode != KeyEvent.KEYCODE_MENU && keyCode != KeyEvent.KEYCODE_CALL
                && keyCode != KeyEvent.KEYCODE_ENDCALL;
        if (isInPlaybackState() && isKeyCodeSupported && mMediaController != null) {
            if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK
                    || keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE) {
                if (mMediaPlayer.isPlaying()) {
                    pause();
                    mMediaController.show();
                } else {
                    start();
                    mMediaController.hide();
                }
                return true;
            } else if (keyCode == KeyEvent.KEYCODE_MEDIA_STOP && mMediaPlayer.isPlaying()) {
                pause();
                mMediaController.show();
            } else {
                toggleMediaControlsVisiblity();
            }
        }

        return super.onKeyDown(keyCode, event);
    }

    public int getLastSeekWhenPrepared() {
        return lastSeekWhenPrepared;
    }

    private void toggleMediaControlsVisiblity() {
        if (mMediaController.isShowing()) {
            mMediaController.hide();
        } else {
            mMediaController.show();
        }
    }

    public void start() {
        if (!enforcementWait && !enforcementPause) {
            if (isInPlaybackState()) {
                mMediaPlayer.start();
                mCurrentState = MediaPlayerStatus.STATE_PLAYING;
                StateChange(mCurrentState);
            }
        } else {
            StateChange(MediaPlayerStatus.STATE_ENFORCEMENT);
        }
        mTargetState = MediaPlayerStatus.STATE_PLAYING;
    }

    public void pause() {
        if (isInPlaybackState()) {
            if (mMediaPlayer.isPlaying()) {
                mMediaPlayer.pause();
                mCurrentState = MediaPlayerStatus.STATE_PAUSED;
                StateChange(mCurrentState);
            }
        }
        mTargetState = MediaPlayerStatus.STATE_PAUSED;
    }


    public boolean toggleScreen() {
        invalateScreenSize();
        return fullScreen;
    }

    // cache duration as mDuration for faster access
    public int getDuration() {
        if (isInPlaybackState()) {
            if (mDuration > 0) {
                return mDuration;
            }
            mDuration = mMediaPlayer.getDuration();
            return mDuration;
        }
        mDuration = -1;
        return mDuration;
    }

    public int getCurrentPosition() {
        if (isInPlaybackState()) {
            return mMediaPlayer.getCurrentPosition();
        }
        return 0;
    }

    public void seekTo(int msec) {
        if (msec < 1000) {
            msec = 1000;
        }
        if (isInPlaybackState()) {
            mMediaPlayer.seekTo(msec);
            mSeekWhenPrepared = 0;
            lastSeekWhenPrepared = 0;
        } else {
            mSeekWhenPrepared = msec;
            lastSeekWhenPrepared = 0;
        }
    }

    public void forward() {
        seekTo(getCurrentPosition() + FORWARD_TIME);
    }

    public void rewind() {
        seekTo(getCurrentPosition() - REWIND_TIME);
    }

    public boolean isPlaying() {
        return isInPlaybackState() && mMediaPlayer.isPlaying();
    }

    public boolean isPaused() {
        return mCurrentState != MediaPlayerStatus.STATE_PAUSED;
    }

    public int getBufferPercentage() {
        if (mMediaPlayer != null) {
            return mCurrentBufferPercentage;
        }
        return 0;
    }

    public boolean isInPlaybackState() {
        return (mMediaPlayer != null && mCurrentState != MediaPlayerStatus.STATE_ERROR && mCurrentState != MediaPlayerStatus.STATE_IDLE && mCurrentState != MediaPlayerStatus.STATE_PREPARING);
    }

    public boolean canPause() {
        return mCanPause;
    }

    public boolean canSeekBackward() {
        return mCanSeekBack;
    }

    public boolean canSeekForward() {
        return mCanSeekForward;
    }

    @Override
    public int getAudioSessionId() {
        return 0;
    }

    private void StateChange(int mCurrentState) {
        if (mStateChangeListener != null) {
            mStateChangeListener.onChange(mCurrentState);
        }
    }

    public VideoViewStateChangeListener getStateChangeListener() {
        return mStateChangeListener;
    }

    public void setStateChangeListener(VideoViewStateChangeListener mStateChangeListener) {
        this.mStateChangeListener = mStateChangeListener;
    }

    public interface VideoViewStateChangeListener {
        public void onChange(int mCurrentState);
    }

    public boolean isEnforcementWait() {
        return enforcementWait;
    }

    public void setEnforcementWait(boolean enforcementWait) {
        this.enforcementWait = enforcementWait;
    }

    public boolean isEnforcementPause() {
        return enforcementPause;
    }

    public void setEnforcementPause(boolean enforcementPause) {
        this.enforcementPause = enforcementPause;
    }
}
